<?php
 include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."pagesettings/master.inc");
include(DOCINCLUDES."global_files/global_js_form.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
include(DOCINCLUDES."global_files/global_report.inc");
include(DOCINCLUDES."global_files/global_errorpage.inc");
// if($_POST)
//         {
//         $company_id=$_POST["company_id"];
//         }
if($_GET)
        {
        $company_id=$_GET["company_id"];
	$company_str=$_GET["company_str"];
        unset($_GET);
        }
// $form = new cForm();
// $db     = new cDatabase($company_id);
// $sql    = new pgSql();
$report = new cReport();
$report->sql=$sql;
$report->db=$db;
$dv=new cDataValidation();
if($_GET['action']=='delete')
{

$db->query="Delete from $company_contact where company_id=$company_id";
$db->runQuery();

}

if($_POST)
	{
	$dv->validations["company_id"]=array("isEmpty"=>"Select Company");
	$dv->validate($_POST);
		if(!$dv->errors)
			{
			$db->beginTransaction();
			$commercial_details["company_id"]=$_POST["company_id"];
			if($_POST["companyname"])
			$commercial_details["company_name"]=$_POST["companyname"];
			if($_POST["address"])
			$commercial_details["address"]=$_POST["address"];
			if($_POST["city_id"])
			$commercial_details["city_name"]=$_POST["city_id"];
			if($_POST["state_id"])
			$commercial_details["state"]=$_POST["state_id"];
			if($_POST["country_id"])
			$commercial_details["country_name"]=$_POST["country_id"];
			if($_POST["zip_code"])
			$commercial_details["zipcode"]=$_POST["zip_code"];
			if($_POST["tax_detail"])
			$commercial_details["tax_detail"]="{".$_POST["tax_detail"]."}";
			if($_POST["ph_no"])
			$commercial_details["phone_number"]="{".$_POST["ph_no"]."}";
			if($_POST["fax_no"])
			$commercial_details["fax_number"]="{".$_POST["fax_no"]."}";
			if($_POST["email"])
			$commercial_details["email_address"]="{".$_POST["email"]."}";
			if($_POST["website"])
			$commercial_details["website"]="{".$_POST["website"]."}";
			
			$sql->table ="company_contact";
			$sql->column =$commercial_details;
			$sql->generateInsertQuery();
			$db->query = $sql->query;
			$db->runQuery();
	
			if(!$db->errors)
				{
				$db->endTransaction();
				unset($_POST);
				header("Location:master_company_contact.php");
				exit;
				} 
			}
	}
$page->title=$page->heading="Company Contact Details";
$page->doHeaderHtml();
	echo "<br/><div align=\"center\">";
	echo "<a href=\"master_relationship.php?rel_type=buy\">Buyer Details</a> || ";
	echo "<a href=\"master_relationship.php?rel_type=act\">Account Details</a> || <a href=\"master_company_contact.php?company_id=$company_id\"&action='delete'> Delete</a>";
if(!$company_id)
{
	$form->properties = array("name"=>"master_company_contact","method"=>"POST","action"=>$_SERVER["PHP_SELF"]);
	echo $form->startForm();

	$db->query="select company_id,1,company_name from _100351._mcompany where company_id not in (SELECT company_id FROM _100351.company_contact) and company_name !='NO BUYER'";
	$db->runQuery();
	$company_name_array=$db->returnArrays();

	$form->properties = array("name"=>"company_id","onChange"=>"self.location.href=\"master_company_contact.php?company_str=\"+this.value");
	$form->select_tag_properties=array("arrays"=>$company_name_array,"default"=>$company_str);
	$item_list[0]["Company Name"] = $form->generateSelectTag();
	$item_list[0]["Company Name"].= $dv->errors["company_id"]; 
//////////////////////////
	if ($company_str)
	{
		$db->query="select company_name from _100351._mcompany where company_id =$company_str";
		$db->runQuery();
		$selected_company_name=$db->returnArrays();
		$company_str=$selected_company_name[0]['company_name'];
	}
///////////////////////////

	$form->properties =array("name"=>"companyname","type"=>"text","size"=>"35","value"=>$company_str);
	$item_list[0]["Display Name"] = $form->generateInputTag();

	$form->properties = array("name"=>"address","type"=>"text","size"=>"35");
	$item_list[0]["Address"] = $form->generateInputTag();
	
	$city_array=getMaster("city");
	$form->properties = array("name"=>"city_id");
	$form->select_tag_properties=array("arrays"=>$city_array);
	$item_list[0]["City"] = $form->generateSelectTag();
	
	$state_array=getMaster("state");
	$form->properties = array("name"=>"state_id");
	$form->select_tag_properties=array("arrays"=>$state_array);
	$item_list[0]["State"] = $form->generateSelectTag();
	
	$country_array=getMaster("country");
	$form->properties = array("name"=>"country_id");
	$form->select_tag_properties=array("arrays"=>$country_array);
	$item_list[0]["Country"] = $form->generateSelectTag();
	
	$form->properties = array("name"=>"zip_code","type"=>"text","size"=>"20");
	$item_list[0]["Zip Code"] = $form->generateInputTag();
	
	$form->properties = array("name"=>"tax_detail","type"=>"text","size"=>"20");
	$item_list[0]["Tax Info"] = $form->generateInputTag();
	
	$form->properties = array("name"=>"ph_no","type"=>"text","size"=>"20");
	$item_list[0]["Phone No."] = $form->generateInputTag();
	
	$form->properties = array("name"=>"fax_no","type"=>"text","size"=>"20");
	$item_list[0]["Fax No."] = $form->generateInputTag();
	
	$form->properties = array("name"=>"email","type"=>"text","size"=>"20");
	$item_list[0]["Email"] = $form->generateInputTag();
	
	$form->properties = array("name"=>"website","type"=>"text","size"=>"20");
	$item_list[0]["Website"] = $form->generateInputTag();
	
	$report->customization["table_style"]="width:30%; border-style:solid; border-width:thin; border-collapse:collapse;";
	$report->customization["tr_style"]="border-style:none; border-width:thin; border-collapse:collapse";
	$report->title="Company Contact Entry";
	$report->highlight=true;
	$report->data=$item_list;
	$report->doVerticalTable();
	$form->properties = array("name"=>"save","type"=>"submit","Value"=>"Save","onclick"=>"testconform()");
	echo $form->generateInputTag();
	echo $form->endForm();
}

if($company_id)
	{
	$db->query="SELECT mco.company_name as \"Company\",cc.company_name as \"Company Name \",cc.address as \"Address\",mct.city_name as \"City\",ms.state_name as \"State\",mcy.country_name as \"Country\",cc.zipcode as \"Zipcode\",cc.tax_detail[1] as \"Tax Detail\",cc.phone_number[1] as \"Ph No.\",cc.fax_number[1] as \"Fax No.\",cc.email_address[1] as \"Email\",cc.website[1] as \"Website\" from company_contact cc join _mcompany mco on mco.company_id=cc.company_id left join _mcity mct on mct.city_id=cc.city_name left join _mcountry mcy on mcy.country_id=cc.country_name left join _mstate ms on ms.state_id=cc.state where mco.company_id=$company_id order by cc.contact_id;";
	}
else
	{
	$db->query="SELECT mco.company_name as \"Company\",cc.company_name as \"Company Name \",cc.address as \"Address\",mct.city_name as \"City\",ms.state_name as \"State\",mcy.country_name as \"Country\",cc.zipcode as \"Zipcode\",cc.tax_detail[1] as \"Tax Detail\",cc.phone_number[1] as \"Ph No.\",cc.fax_number[1] as \"Fax No.\",cc.email_address[1] as \"Email\",cc.website[1] as \"Website\" from company_contact cc join _mcompany mco on mco.company_id=cc.company_id left join _mcity mct on mct.city_id=cc.city_name left join _mcountry mcy on mcy.country_id=cc.country_name left join _mstate ms on ms.state_id=cc.state order by mco.company_id;";
	}
	$db->runQuery();
	$view_list=$db->returnArrays();
	$report->customization["table_style"]="width:80%; border-style:solid; border-width:thin; border-collapse:collapse;";
	$report->customization["tr_style"]="border-style:none; border-width:thin; border-collapse:collapse";
	$report->title="Company Contact Details";
	$report->data=$view_list;
	echo "</br>";
	echo "</br>";
	$report->setNA();
	$report->doHorizontalTable();
	echo "</div><br />";

$page->doFooterHtml();
?>
<script type="text/Javascript">
function testconform()
{	
	var ok = confirm("Do you want to save");
}
</script>
